<?php

namespace app\common\model\pay;



use app\api\model\hotel\HotelThirdOrder;
use fast\Http;
use fast\Random;

class PayShare
{

    public $cusRsaPrivateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCh1Sr63Vg9qAs2mD3A3mbYnInWwAhjWIiVveeqZIgpW8D7RQRoxGPk4fJDyAUfusBSDBjCOTl09F4JHT33RJmaROQ/BRaEscMcEM5hZwrTxdkZ/r91hPMzndL6RQy2Swtv4h0Njp10gSiBPD7cZgd1NxmXd29fLGkIPJg4MAiS/qZESyA3kSKYxFfzYzEQrfbFjzZ+/GAQ47rxQGx2AN5nogRtiByXMvKBP0tx85xMddYpkogWOSS0+5ZXeKg7+00sOlyfXGCPWSiALH1ld6zONt8eM0EKsGIhpOe76mbOejCVQs9/MKoV5NaJNP4HoFkeDSVS5abtbfjjsoTI5ZRbAgMBAAECggEAER++ndpGDWAhWkmJtJrT7x/BRhlTyeStrzVFCjamYcSftaF78fS19/DLSHjQcfxk1Um1D6LWfZWjTaZBJE+nS+JLnzopQ9PmKADElcc9fLh2sdV5lN8t4iGqbWkDtgEC2M09ncL1eO/McBWxNnO6s6B882tj5teK1aBJIHOUtK5U7aKZNLL7h9fWQmdSIc5VlQK6x0OrV8OyJI0VG6RammUyhpaCmRHof6Sx/T18QYnNqQzBMbXYwZaEJsrQzQO6UEHPHZsfJpLcp326O8m7Gd+wWfNoDmryEkjNjr7Gmy/HRwKqX9ICK6SOCZYY65CB7NAlcZyMc5mldt/otBm8AQKBgQDXC4joWgQ0RhOErxe+r9oFBTeVqhI1g9Qp8051pw8zrj507mmwZIq3O72hOjZ0zagUjjXBTRYvKCq8weOvXOt3tVwOxKOWlpOTi1OP0Qn0fW4BggCVyNm+Y8okfmztyz7wn+YVWw8VKcCH1+iWOYovrZ8LDsV8DyVssjhK+4LogQKBgQDAp0colWKXltYbSxQA+t+VSeeCiUUH641OPR/svZBr0Pkox5VlXyJweMBDa+I2P6BBk0ylxAD0Y74uGSOuAxKEP+7RAGRYsz8KEMQxAZpOZ+oCBclcF1kbc58Dija9s8x1zVKihYaoc5jAj5OvELqOJXzu74oD+lxLmH8EDWGu2wKBgQCkZt4g/IiQSi6r1oxprNkZ03Y+RkorfYBbHfaSvchCc2iuToKgZw0hrlt4AiAVxhBEvr/b6woVvTiHHBfsPjaKSy144N5LxwxF1VNp9J8OukG6h1/ZIzdMdc/ZfIncdfDFVwk4tGfgfw4BDJB1IMJDVxHrCzzxbkbnRY0BziyzgQKBgCd6oBRq+HdEC8AwFHtW7bZ0HKMdai4DUUO1NvOWrR/LBSQcMjtB0Zbmx8+JLiQbycVmYBCgJ+ns/q4HrM4eBw9e4pulzup2YPa5FNtODsb+eG/Nq8PZbQM5IF7jVfBuBfomB0cSwdOHTMg5I/R+Ixy6Be4Y3FmQVpWPPIgiDeB9AoGBAI78NOXY8DrTmzaO8qh+OHFEP4heR/hrNmR3srMeaQomiZqvcDkkZGsUbB4wtseA7KzjLvZ8LBR7ynOS6AA9KM9uRMcsDXMpdpNgwqPs6zZs67IMWCyKlm8MJqDH8FTdx77Tzix2efeK3bOit0ad/5pFQ2Dnfrac7zXXFuErgQY4";

    public $appid = "00377402";

    //public $cusid = "56433607011JKNZ";

    public $orgid = "56433607011JKNW";

    //杭州背包客酒店管理有限公司
    //商户号：56433607011JKP1

    //杭州零点青年酒店管理有限公司
    //56433607011JKNZ

    //分账，交易由上面2个商户号拉起支付，分账给旅小二的商户号   56433607011JKNW、杭州旅小二酒店管理有限公司

    //分账业务中，接口的orgid的值：56433607011JKNW

    //支付
    public function orderpay($order_no)
    {
        $order = HotelThirdOrder::get(['order_no'=>$order_no]);
        $reqsn = $order['order_no'];
        $request = request();

        $notify_url = $request->root(true) . '/api/hotelorder/notify';

        $auto_close = \app\common\model\Config::get(['name'=>'close_time'])->value*60;

        $expiretime = $order['createtime']+$auto_close;
        //$remark = '';
        $trxamt = $order['pay_money']*100;

        $cusid = "56433607011JKNZ";

        $data = [
            'cusid'=>$cusid,
            'orgid'=>$this->orgid,
            'appid'=>$this->appid,
            'version'=>'12',
            'trxamt'=>(string)$trxamt,
            'reqsn'=>$reqsn,
            'expiretime'=>date('YmdHis',$expiretime),
            'notify_url'=>$notify_url,
            'body'=>'订单号:'.$reqsn,
            //'remark'=>$remark,
            'validtime'=>'30',
            'paytype'=>'W06',
            'limit_pay'=>'no_credit',
            //'multipay'=>'1',
            //'asinfo'=>'',
            'randomstr'=>Random::alnum(32),
            //'subbranch'=>$this->subbranch,
            'signtype'=>'RSA',
            //'isdirectpay'=>1,
            //'ishideshare'=>1,
            //'isdirectback'=>1,
            //'idno'=>$idno,
            //'truename'=>$truename,
        ];

        $util = new AppUtil();

        $sign = $util::Sign($data,$this->cusRsaPrivateKey);

        $data['sign'] = $sign;

        return $data;
    }

    //分账
    public function share($order_no)
    {
        $order = HotelThirdOrder::get(['order_no'=>$order_no]);

        $url = 'https://vsp.allinpay.com/apiweb/trxshare/share';
        //$testurl = 'https://syb-test.allinpay.com/apiweb/trxshare/share';

        $payeereqsn = date("Ymdhis") . sprintf("%08d", $order['hotel_id']) . mt_rand(1000, 9999);

        $reqsn = date('YmdHis') . mt_rand(1000, 9999);

        $trxamt = $order['pay_money']*100;

        $cusid = "56433607011JKNZ";

        $data = [
            'orgid'=>$this->orgid,
            'appid'=>$this->appid,
            'cusid'=>$cusid,
            'version'=>12,
            'randomstr'=>Random::alnum(32),
            'signtype'=>'RSA',
            'reqsn'=>$reqsn,
            'payeecusid'=>$this->orgid,
            'payeereqsn'=>$payeereqsn,
            'trxamt'=>$trxamt,
            'oldtrxid'=>$order['trxid'],
            'remark'=>'分账'
        ];

        $util = new AppUtil();

        $sign = $util::Sign($data,$this->cusRsaPrivateKey);

        $data['sign'] = $sign;

        $res = Http::post($url,$data);
        dump($data);
        dump(json_encode($data,320));
        dump($res);die;
        $result = json_decode($res,true);

        if ($result['retcode']=='FAIL'){
            throw new \Exception($result['retmsg']);
        } else {
            return $result;
        }

    }


}